
package com.lineage.server.serverpackets;

import static com.lineage.server.model.skill.L1SkillId.STATUS_BRAVE3;

import java.util.Random;

import com.lineage.server.model.Instance.L1PcInstance;

/**
 * 物件封包 - 本身
 * @author dexc
 *
 */
public class S_OwnCharPack extends ServerBasePacket {

	//private static final int STATUS_POISON = 1;
	private static final int STATUS_INVISIBLE = 2;
	private static final int STATUS_PC = 4;
	//private static final int STATUS_FREEZE = 8;
	private static final int STATUS_BRAVE = 16;
	private static final int STATUS_ELFBRAVE = 32;
	private static final int STATUS_FASTMOVABLE = 64;
	private static final int STATUS_GHOST = 128;
	
	public static final Random _random = new Random();
	
	private byte[] _byte = null;

	/**
	 * 物件封包 - 本身
	 * @param pc
	 */
	public S_OwnCharPack(final L1PcInstance pc) {
		this.buildPacket(pc);
	}

	private void buildPacket(final L1PcInstance pc) {
		int status = STATUS_PC;

		if (pc.isInvisble() || pc.isGmInvis()) {
			status |= STATUS_INVISIBLE;
		}
		if (pc.isBrave()) {
			status |= STATUS_BRAVE;
		}
		if (pc.isElfBrave()) {
			// エルヴンワッフルの場合は、STATUS_BRAVEとSTATUS_ELFBRAVEを立てる。
			// STATUS_ELFBRAVEのみでは効果が無い？
			status |= STATUS_BRAVE;
			status |= STATUS_ELFBRAVE;
		}
		if (pc.isFastMovable()) {
			status |= STATUS_FASTMOVABLE;
		}
		if (pc.isGhost()) {
			status |= STATUS_GHOST;
		}

		this.writeC(S_OPCODE_CHARPACK);
		this.writeH(pc.getX());
		this.writeH(pc.getY());
		this.writeD(pc.getId());

		if (pc.isDead()) {
			this.writeH(pc.getTempCharGfxAtDead());
			
		} else {
			this.writeH(pc.getTempCharGfx());
		}

		if (pc.isDead()) {
			this.writeC(pc.getStatus());
		} else {
			this.writeC(pc.getCurrentWeapon());
		}

		this.writeC(pc.getHeading());
		// writeC(addbyte);
		this.writeC(pc.getOwnLightSize());
		this.writeC(pc.getMoveSpeed());
		this.writeD((int) pc.getExp());
		this.writeH(pc.getLawful());

		// TODO 大豪 轉職頭銜-自身顯示
		String title = "";
	     if (pc.getLevelClass() == 1 && pc.getLevelClasslv() >= 1000) {
		  title ="\\f=【光】";
	    }
	     else if (pc.getLevelClass() == 1 && pc.getLevelClasslv() >= 2500) {
		  title ="\\f=【神族】";
	    }
	     else if (pc.getLevelClass() == 1 && pc.getLevelClasslv() >= 4999) {
		  title ="\\f=【玄境】";
	    }
	     else if (pc.getLevelClass() == 2 && pc.getLevelClasslv() >= 1000) {
		  title ="\\f=【暗】";
	    }
	     else if (pc.getLevelClass() == 2 && pc.getLevelClasslv() >= 2500) {
		  title ="\\f=【魔族】";
	    }
	     else if (pc.getLevelClass() == 2 && pc.getLevelClasslv() >= 4999) {
		  title ="\\f=【極魔】";
	    }
	     
	     
		final StringBuilder stringBuilder = new StringBuilder();
		if (pc.get_other().get_color() != 0) {
			stringBuilder.append(pc.get_other().color());
		}
		
		//轉職頭銜 GM / 玩家
		if (pc.isGm()) {
			stringBuilder.append("\\f="+pc.getName());
		} else {
		    stringBuilder.append(pc.getName() + title);
	    }
		
		this.writeS(stringBuilder.toString());
		this.writeS(pc.getTitle());
		this.writeC(status); // 狀態
		this.writeD(pc.getClanid());
		this.writeS(pc.getClanname()); // 血盟名稱
		this.writeS(null); // 主人名稱

		// 0:NPC,道具 
		// 1:中毒 ,
		// 2:隱身 
		// 4:人物
		// 8:詛咒 
		// 16:勇水
		// 32:??
		// 64:??(??)
		// 128:invisible but name
		this.writeC(pc.getClanRank() << 4);// 血盟階級
		
		if (pc.isInParty()) {// 隊伍中
			this.writeC(100 * pc.getCurrentHp() / pc.getMaxHp()); // HP顯示
			
		} else {
			this.writeC(0xff); // HP顯示
		}
		if (pc.hasSkillEffect(STATUS_BRAVE3)) {
			this.writeC(0x08); // 巧克力蛋糕
			
		} else {
			this.writeC(0x00); // タルクック距離(通り)
		}
		this.writeC(0x00); // LV
		this.writeC(0x00);
		this.writeC(0xff);
		this.writeC(0xff);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this.getBytes();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return this.getClass().getSimpleName();
	}
}